<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:yui="http://yuilibrary.com/rdf/1.0/yui.rdf#">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>API: json   JSON.js  (YUI Library)</title>

	<link rel="stylesheet" type="text/css" href="assets/reset-fonts-grids-min.css" />
	<link rel="stylesheet" type="text/css" href="assets/api.css" />

    <script type="text/javascript" src="assets/api-js"></script>
    <script type="text/javascript" src="assets/ac-js"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">
	<div id="hd">
        <h1><a href="http://developer.yahoo.com/yui/" title="Yahoo! UI Library">Yahoo! UI Library</a></h1>
        <h3>json&nbsp; <span class="subtitle">2.7.0</span></h3>
        <a href="./index.html" title="Yahoo! UI Library">Yahoo! UI Library</a> 
            &gt; <a href="./module_json.html" title="json">json</a>
                
                 &gt; JSON.js (source view) 
        <form onsubmit="return false">
            <div id="propertysearch">
                Search: <input autocomplete="off" id="searchinput" />
                <div id="searchresults">
                    &nbsp;
                </div>
            </div>
        </form>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
                <fieldset>
                    <legend>Filters</legend>
                <span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
                <span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
                <span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
                </fieldset>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 .classopts { display:none; }
                        </style>
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * Provides methods to parse JSON strings and convert objects to JSON strings.</span>
<span class="c"> * @module json</span>
<span class="c"> * @class JSON</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">YAHOO</span><span class="o">.</span><span class="nx">lang</span><span class="o">.</span><span class="nx">JSON</span> <span class="o">=</span> <span class="o">(</span><span class="k">function</span> <span class="o">()</span> <span class="o">{</span>

<span class="k">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">YAHOO</span><span class="o">.</span><span class="nx">lang</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * Replace certain Unicode characters that JavaScript may handle incorrectly</span>
<span class="c">     * during eval--either by deleting them or treating them as line</span>
<span class="c">     * endings--with escape sequences.</span>
<span class="c">     * IMPORTANT NOTE: This regex will be used to modify the input if a match is</span>
<span class="c">     * found.</span>
<span class="c">     * @property _UNICODE_EXCEPTIONS</span>
<span class="c">     * @type {RegExp}</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_UNICODE_EXCEPTIONS</span> <span class="o">=</span> <span class="sr">/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * First step in the validation.  Regex used to replace all escape</span>
<span class="c">     * sequences (i.e. &quot;\\&quot;, etc) with &#39;@&#39; characters (a non-JSON character).</span>
<span class="c">     * @property _ESCAPES</span>
<span class="c">     * @type {RegExp}</span>
<span class="c">     * @static</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_ESCAPES</span> <span class="o">=</span> <span class="sr">/\\(?:[&quot;\\\/bfnrt]|u[0-9a-fA-F]{4})/g</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * Second step in the validation.  Regex used to replace all simple</span>
<span class="c">     * values with &#39;]&#39; characters.</span>
<span class="c">     * @property _VALUES</span>
<span class="c">     * @type {RegExp}</span>
<span class="c">     * @static</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_VALUES</span>  <span class="o">=</span> <span class="sr">/&quot;[^&quot;\\\n\r]*&quot;|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * Third step in the validation.  Regex used to remove all open square</span>
<span class="c">     * brackets following a colon, comma, or at the beginning of the string.</span>
<span class="c">     * @property _BRACKETS</span>
<span class="c">     * @type {RegExp}</span>
<span class="c">     * @static</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_BRACKETS</span> <span class="o">=</span> <span class="sr">/(?:^|:|,)(?:\s*\[)+/g</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * Final step in the validation.  Regex used to test the string left after</span>
<span class="c">     * all previous replacements for invalid characters.</span>
<span class="c">     * @property _INVALID</span>
<span class="c">     * @type {RegExp}</span>
<span class="c">     * @static</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_INVALID</span>  <span class="o">=</span> <span class="sr">/^[\],:{}\s]*$/</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * Regex used to replace special characters in strings for JSON</span>
<span class="c">     * stringification.</span>
<span class="c">     * @property _SPECIAL_CHARS</span>
<span class="c">     * @type {RegExp}</span>
<span class="c">     * @static</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_SPECIAL_CHARS</span> <span class="o">=</span> <span class="sr">/[\\\&quot;\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g</span><span class="o">,</span>

    <span class="c">/**</span>
<span class="c">     * Character substitution map for common escapes and special characters.</span>
<span class="c">     * @property _CHARS</span>
<span class="c">     * @type {Object}</span>
<span class="c">     * @static</span>
<span class="c">     * @private</span>
<span class="c">     */</span>
    <span class="nx">_CHARS</span> <span class="o">=</span> <span class="o">{</span>
        <span class="s1">&#39;\b&#39;</span><span class="o">:</span> <span class="s1">&#39;\\b&#39;</span><span class="o">,</span>
        <span class="s1">&#39;\t&#39;</span><span class="o">:</span> <span class="s1">&#39;\\t&#39;</span><span class="o">,</span>
        <span class="s1">&#39;\n&#39;</span><span class="o">:</span> <span class="s1">&#39;\\n&#39;</span><span class="o">,</span>
        <span class="s1">&#39;\f&#39;</span><span class="o">:</span> <span class="s1">&#39;\\f&#39;</span><span class="o">,</span>
        <span class="s1">&#39;\r&#39;</span><span class="o">:</span> <span class="s1">&#39;\\r&#39;</span><span class="o">,</span>
        <span class="s1">&#39;&quot;&#39;</span> <span class="o">:</span> <span class="s1">&#39;\\&quot;&#39;</span><span class="o">,</span>
        <span class="s1">&#39;\\&#39;</span><span class="o">:</span> <span class="s1">&#39;\\\\&#39;</span>
    <span class="o">};</span>

<span class="c">/**</span>
<span class="c"> * Traverses nested objects, applying a filter or reviver function to</span>
<span class="c"> * each value.  The value returned from the function will replace the</span>
<span class="c"> * original value in the key:value pair.  If the value returned is</span>
<span class="c"> * undefined, the key will be omitted from the returned object.</span>
<span class="c"> * @method _revive</span>
<span class="c"> * @param data {MIXED} Any JavaScript data</span>
<span class="c"> * @param reviver {Function} filter or mutation function</span>
<span class="c"> * @return {MIXED} The results of the filtered/mutated data structure</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">function</span> <span class="nx">_revive</span><span class="o">(</span><span class="nx">data</span><span class="o">,</span> <span class="nx">reviver</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">var</span> <span class="nx">walk</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">key</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">k</span><span class="o">,</span><span class="nx">v</span><span class="o">,</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">key</span><span class="o">];</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">value</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">value</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">for</span> <span class="o">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">value</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">if</span> <span class="o">(</span><span class="nx">l</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">value</span><span class="o">,</span><span class="nx">k</span><span class="o">))</span> <span class="o">{</span>
                    <span class="nx">v</span> <span class="o">=</span> <span class="nx">walk</span><span class="o">(</span><span class="nx">value</span><span class="o">,</span> <span class="nx">k</span><span class="o">);</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nx">v</span> <span class="o">===</span> <span class="kc">undefined</span><span class="o">)</span> <span class="o">{</span>
                        <span class="nx">delete</span> <span class="nx">value</span><span class="o">[</span><span class="nx">k</span><span class="o">];</span>
                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                        <span class="nx">value</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
                    <span class="o">}</span>
                <span class="o">}</span>
            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nx">reviver</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">key</span><span class="o">,</span><span class="nx">value</span><span class="o">);</span>
    <span class="o">};</span>

    <span class="k">return</span> <span class="k">typeof</span> <span class="nx">reviver</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">walk</span><span class="o">({</span><span class="s1">&#39;&#39;</span><span class="o">:</span><span class="nx">data</span><span class="o">},</span><span class="s1">&#39;&#39;</span><span class="o">)</span> <span class="o">:</span> <span class="nx">data</span><span class="o">;</span>
<span class="o">}</span>

<span class="c">/**</span>
<span class="c"> * Escapes a special character to a safe Unicode representation</span>
<span class="c"> * @method _char</span>
<span class="c"> * @param c {String} single character to escape</span>
<span class="c"> * @return {String} safe Unicode escape</span>
<span class="c"> */</span>
<span class="k">function</span> <span class="nx">_char</span><span class="o">(</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(!</span><span class="nx">_CHARS</span><span class="o">[</span><span class="nx">c</span><span class="o">])</span> <span class="o">{</span>
        <span class="nx">_CHARS</span><span class="o">[</span><span class="nx">c</span><span class="o">]</span> <span class="o">=</span>  <span class="s1">&#39;\\u&#39;</span><span class="o">+(</span><span class="s1">&#39;0000&#39;</span><span class="o">+(+(</span><span class="nx">c</span><span class="o">.</span><span class="nx">charCodeAt</span><span class="o">(</span><span class="m">0</span><span class="o">))).</span><span class="nx">toString</span><span class="o">(</span><span class="m">16</span><span class="o">)).</span><span class="nx">slice</span><span class="o">(-</span><span class="m">4</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="k">return</span> <span class="nx">_CHARS</span><span class="o">[</span><span class="nx">c</span><span class="o">];</span>
<span class="o">}</span>

<span class="c">/**</span>
<span class="c"> * Replace certain Unicode characters that may be handled incorrectly by</span>
<span class="c"> * some browser implementations.</span>
<span class="c"> * @method _prepare</span>
<span class="c"> * @param s {String} parse input</span>
<span class="c"> * @return {String} sanitized JSON string ready to be validated/parsed</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">function</span> <span class="nx">_prepare</span><span class="o">(</span><span class="nx">s</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nx">s</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="nx">_UNICODE_EXCEPTIONS</span><span class="o">,</span> <span class="nx">_char</span><span class="o">);</span>
<span class="o">}</span>

<span class="c">/**</span>
<span class="c"> * Four step determination whether a string is valid JSON.  In three steps,</span>
<span class="c"> * escape sequences, safe values, and properly placed open square brackets</span>
<span class="c"> * are replaced with placeholders or removed.  Then in the final step, the</span>
<span class="c"> * result of all these replacements is checked for invalid characters.</span>
<span class="c"> * @method _isValid</span>
<span class="c"> * @param str {String} JSON string to be tested</span>
<span class="c"> * @return {boolean} is the string safe for eval?</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="k">function</span> <span class="nx">_isValid</span><span class="o">(</span><span class="nx">str</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="nx">l</span><span class="o">.</span><span class="nx">isString</span><span class="o">(</span><span class="nx">str</span><span class="o">)</span> <span class="o">&amp;&amp;</span>
            <span class="nx">_INVALID</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">str</span><span class="o">.</span>
            <span class="nx">replace</span><span class="o">(</span><span class="nx">_ESCAPES</span><span class="o">,</span><span class="s1">&#39;@&#39;</span><span class="o">).</span>
            <span class="nx">replace</span><span class="o">(</span><span class="nx">_VALUES</span><span class="o">,</span><span class="s1">&#39;]&#39;</span><span class="o">).</span>
            <span class="nx">replace</span><span class="o">(</span><span class="nx">_BRACKETS</span><span class="o">,</span><span class="s1">&#39;&#39;</span><span class="o">));</span>
<span class="o">}</span>

<span class="c">/**</span>
<span class="c"> * Enclose escaped strings in quotes</span>
<span class="c"> * @method _string</span>
<span class="c"> * @param s {String} string to wrap</span>
<span class="c"> * @return {String} &#39;&quot;&#39;+s+&#39;&quot;&#39; after s has had special characters escaped</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">function</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">s</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="s1">&#39;&quot;&#39;</span> <span class="o">+</span> <span class="nx">s</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="nx">_SPECIAL_CHARS</span><span class="o">,</span> <span class="nx">_char</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;&quot;&#39;</span><span class="o">;</span>
<span class="o">}</span>

<span class="c">/**</span>
<span class="c"> * Worker function used by public stringify.</span>
<span class="c"> * @method _stringify</span>
<span class="c"> * @param h {Object} object holding the key</span>
<span class="c"> * @param key {String} String key in object h to serialize</span>
<span class="c"> * @param depth {Number} depth to serialize</span>
<span class="c"> * @param w {Array|Function} array of whitelisted keys OR replacer function</span>
<span class="c"> * @param pstack {Array} used to protect against recursion</span>
<span class="c"> * @return {String} serialized version of o</span>
<span class="c"> */</span>
<span class="k">function</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">h</span><span class="o">,</span><span class="nx">key</span><span class="o">,</span><span class="nx">d</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">pstack</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">var</span> <span class="nx">o</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">w</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">w</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">h</span><span class="o">,</span><span class="nx">key</span><span class="o">,</span><span class="nx">h</span><span class="o">[</span><span class="nx">key</span><span class="o">])</span> <span class="o">:</span> <span class="nx">h</span><span class="o">[</span><span class="nx">key</span><span class="o">],</span>
        <span class="nx">i</span><span class="o">,</span><span class="nx">len</span><span class="o">,</span><span class="nx">j</span><span class="o">,</span> <span class="c">// array iteration</span>
<span class="c"></span>        <span class="nx">k</span><span class="o">,</span><span class="nx">v</span><span class="o">,</span>     <span class="c">// object iteration</span>
<span class="c"></span>        <span class="nx">isArray</span><span class="o">,</span> <span class="c">// forking in typeof &#39;object&#39;</span>
<span class="c"></span>        <span class="nx">a</span><span class="o">;</span>       <span class="c">// composition array for performance over string concat</span>
<span class="c"></span>
    <span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">Date</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">o</span> <span class="o">=</span> <span class="nx">l</span><span class="o">.</span><span class="nx">JSON</span><span class="o">.</span><span class="nx">dateToString</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">String</span> <span class="o">||</span> <span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">Boolean</span> <span class="o">||</span> <span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">Number</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">o</span> <span class="o">=</span> <span class="nx">o</span><span class="o">.</span><span class="nx">valueOf</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="nx">switch</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">case</span> <span class="s1">&#39;string&#39;</span> <span class="o">:</span> <span class="k">return</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
        <span class="nx">case</span> <span class="s1">&#39;number&#39;</span> <span class="o">:</span> <span class="k">return</span> <span class="nb">isFinite</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">?</span> <span class="nb">String</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">:</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
        <span class="nx">case</span> <span class="s1">&#39;boolean&#39;</span><span class="o">:</span> <span class="k">return</span> <span class="nb">String</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
        <span class="nx">case</span> <span class="s1">&#39;object&#39;</span> <span class="o">:</span>
            <span class="c">// null</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="o">===</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="c">// Check for cyclical references</span>
<span class="c"></span>            <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="nx">pstack</span><span class="o">.</span><span class="nx">length</span> <span class="o">-</span> <span class="m">1</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&gt;=</span> <span class="m">0</span><span class="o">;</span> <span class="o">--</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">if</span> <span class="o">(</span><span class="nx">pstack</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">return</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
                <span class="o">}</span>
            <span class="o">}</span>

            <span class="c">// Add the object to the processing stack</span>
<span class="c"></span>            <span class="nx">pstack</span><span class="o">[</span><span class="nx">pstack</span><span class="o">.</span><span class="nx">length</span><span class="o">]</span> <span class="o">=</span> <span class="nx">o</span><span class="o">;</span>

            <span class="nx">a</span> <span class="o">=</span> <span class="o">[];</span>
            <span class="nx">isArray</span> <span class="o">=</span> <span class="nx">l</span><span class="o">.</span><span class="nx">isArray</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>

            <span class="c">// Only recurse if we&#39;re above depth config</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">d</span> <span class="o">&gt;</span> <span class="m">0</span><span class="o">)</span> <span class="o">{</span>
                <span class="c">// Array</span>
<span class="c"></span>                <span class="k">if</span> <span class="o">(</span><span class="nx">isArray</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="nx">o</span><span class="o">.</span><span class="nx">length</span> <span class="o">-</span> <span class="m">1</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&gt;=</span> <span class="m">0</span><span class="o">;</span> <span class="o">--</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                        <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">i</span><span class="o">,</span><span class="nx">d</span><span class="o">-</span><span class="m">1</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">pstack</span><span class="o">)</span> <span class="o">||</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
                    <span class="o">}</span>

                <span class="c">// Object</span>
<span class="c"></span>                <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                    <span class="nx">j</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
                    <span class="c">// Use whitelist keys if provided as an array</span>
<span class="c"></span>                    <span class="k">if</span> <span class="o">(</span><span class="nx">l</span><span class="o">.</span><span class="nx">isArray</span><span class="o">(</span><span class="nx">w</span><span class="o">))</span> <span class="o">{</span>
                        <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">w</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nx">k</span> <span class="o">=</span> <span class="nx">w</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
                            <span class="nx">v</span> <span class="o">=</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">k</span><span class="o">,</span><span class="nx">d</span><span class="o">-</span><span class="m">1</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">pstack</span><span class="o">);</span>
                            <span class="k">if</span> <span class="o">(</span><span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
                                <span class="nx">a</span><span class="o">[</span><span class="nx">j</span><span class="o">++]</span> <span class="o">=</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">k</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nx">v</span><span class="o">;</span>
                            <span class="o">}</span>
                        <span class="o">}</span>
                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                        <span class="k">for</span> <span class="o">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
                            <span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">k</span> <span class="o">===</span> <span class="s1">&#39;string&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">l</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">k</span><span class="o">))</span> <span class="o">{</span>
                                <span class="nx">v</span> <span class="o">=</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">k</span><span class="o">,</span><span class="nx">d</span><span class="o">-</span><span class="m">1</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">pstack</span><span class="o">);</span>
                                <span class="k">if</span> <span class="o">(</span><span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
                                    <span class="nx">a</span><span class="o">[</span><span class="nx">j</span><span class="o">++]</span> <span class="o">=</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">k</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nx">v</span><span class="o">;</span>
                                <span class="o">}</span>
                            <span class="o">}</span>
                        <span class="o">}</span>
                    <span class="o">}</span>

                    <span class="c">// sort object keys for easier readability</span>
<span class="c"></span>                    <span class="nx">a</span><span class="o">.</span><span class="nx">sort</span><span class="o">();</span>
                <span class="o">}</span>
            <span class="o">}</span>

            <span class="c">// remove the object from the stack</span>
<span class="c"></span>            <span class="nx">pstack</span><span class="o">.</span><span class="nx">pop</span><span class="o">();</span>

            <span class="k">return</span> <span class="nx">isArray</span> <span class="o">?</span> <span class="s1">&#39;[&#39;</span><span class="o">+</span><span class="nx">a</span><span class="o">.</span><span class="nx">join</span><span class="o">(</span><span class="s1">&#39;,&#39;</span><span class="o">)+</span><span class="s1">&#39;]&#39;</span> <span class="o">:</span> <span class="s1">&#39;{&#39;</span><span class="o">+</span><span class="nx">a</span><span class="o">.</span><span class="nx">join</span><span class="o">(</span><span class="s1">&#39;,&#39;</span><span class="o">)+</span><span class="s1">&#39;}&#39;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="k">return</span> <span class="kc">undefined</span><span class="o">;</span> <span class="c">// invalid input</span>
<span class="c"></span><span class="o">}</span>

<span class="c">// Return the public API</span>
<span class="c"></span><span class="k">return</span> <span class="o">{</span>
    <span class="c">/**</span>
<span class="c">     * Four step determination whether a string is valid JSON.  In three steps,</span>
<span class="c">     * escape sequences, safe values, and properly placed open square brackets</span>
<span class="c">     * are replaced with placeholders or removed.  Then in the final step, the</span>
<span class="c">     * result of all these replacements is checked for invalid characters.</span>
<span class="c">     * @method isValid</span>
<span class="c">     * @param str {String} JSON string to be tested</span>
<span class="c">     * @return {boolean} is the string safe for eval?</span>
<span class="c">     * @static</span>
<span class="c">     */</span>
    <span class="nx">isValid</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">s</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nx">_isValid</span><span class="o">(</span><span class="nx">_prepare</span><span class="o">(</span><span class="nx">s</span><span class="o">));</span>
    <span class="o">},</span>

    <span class="c">/**</span>
<span class="c">     * Parse a JSON string, returning the native JavaScript representation.</span>
<span class="c">     * Only minor modifications from http://www.json.org/json2.js.</span>
<span class="c">     * @param s {string} JSON string data</span>
<span class="c">     * @param reviver {function} (optional) function(k,v) passed each key:value</span>
<span class="c">     *          pair of object literals, allowing pruning or altering values</span>
<span class="c">     * @return {MIXED} the native JavaScript representation of the JSON string</span>
<span class="c">     * @throws SyntaxError</span>
<span class="c">     * @method parse</span>
<span class="c">     * @static</span>
<span class="c">     */</span>
    <span class="nx">parse</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">s</span><span class="o">,</span><span class="nx">reviver</span><span class="o">)</span> <span class="o">{</span>
        <span class="c">// sanitize</span>
<span class="c"></span>        <span class="nx">s</span> <span class="o">=</span> <span class="nx">_prepare</span><span class="o">(</span><span class="nx">s</span><span class="o">);</span>

        <span class="c">// Ensure valid JSON</span>
<span class="c"></span>        <span class="k">if</span> <span class="o">(</span><span class="nx">_isValid</span><span class="o">(</span><span class="nx">s</span><span class="o">))</span> <span class="o">{</span>
            <span class="c">// Eval the text into a JavaScript data structure, apply the</span>
<span class="c"></span>            <span class="c">// reviver function if provided, and return</span>
<span class="c"></span>            <span class="k">return</span> <span class="nx">_revive</span><span class="o">(</span> <span class="nb">eval</span><span class="o">(</span><span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">s</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="o">),</span> <span class="nx">reviver</span> <span class="o">);</span>
        <span class="o">}</span>

        <span class="c">// The text is not valid JSON</span>
<span class="c"></span>        <span class="k">throw</span> <span class="k">new</span> <span class="nx">SyntaxError</span><span class="o">(</span><span class="s1">&#39;parseJSON&#39;</span><span class="o">);</span>
    <span class="o">},</span>

    <span class="c">/**</span>
<span class="c">     * Converts an arbitrary value to a JSON string representation.</span>
<span class="c">     * Cyclical object or array references are replaced with null.</span>
<span class="c">     * If a whitelist is provided, only matching object keys will be included.</span>
<span class="c">     * If a depth limit is provided, objects and arrays at that depth will</span>
<span class="c">     * be stringified as empty.</span>
<span class="c">     * @method stringify</span>
<span class="c">     * @param o {MIXED} any arbitrary object to convert to JSON string</span>
<span class="c">     * @param w {Array|Function} (optional) whitelist of acceptable object keys to include OR a function(value,key) to alter values before serialization</span>
<span class="c">     * @param d {number} (optional) depth limit to recurse objects/arrays (practical minimum 1)</span>
<span class="c">     * @return {string} JSON string representation of the input</span>
<span class="c">     * @static</span>
<span class="c">     */</span>
    <span class="nx">stringify</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="o">!==</span> <span class="kc">undefined</span><span class="o">)</span> <span class="o">{</span>
            <span class="c">// Ensure whitelist keys are unique (bug 2110391)</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">l</span><span class="o">.</span><span class="nx">isArray</span><span class="o">(</span><span class="nx">w</span><span class="o">))</span> <span class="o">{</span>
                <span class="nx">w</span> <span class="o">=</span> <span class="o">(</span><span class="k">function</span> <span class="o">(</span><span class="nx">a</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">var</span> <span class="nx">uniq</span><span class="o">=[],</span><span class="nx">map</span><span class="o">={},</span><span class="nx">v</span><span class="o">,</span><span class="nx">i</span><span class="o">,</span><span class="nx">j</span><span class="o">,</span><span class="nx">len</span><span class="o">;</span>
                    <span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">j</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o">&lt;</span><span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                        <span class="nx">v</span> <span class="o">=</span> <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
                        <span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">v</span> <span class="o">===</span> <span class="s1">&#39;string&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">map</span><span class="o">[</span><span class="nx">v</span><span class="o">]</span> <span class="o">===</span> <span class="kc">undefined</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nx">uniq</span><span class="o">[(</span><span class="nx">map</span><span class="o">[</span><span class="nx">v</span><span class="o">]</span> <span class="o">=</span> <span class="nx">j</span><span class="o">++)]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
                        <span class="o">}</span>
                    <span class="o">}</span>
                    <span class="k">return</span> <span class="nx">uniq</span><span class="o">;</span>
                <span class="o">})(</span><span class="nx">w</span><span class="o">);</span>
            <span class="o">}</span>

            <span class="c">// Default depth to POSITIVE_INFINITY</span>
<span class="c"></span>            <span class="nx">d</span> <span class="o">=</span> <span class="nx">d</span> <span class="o">&gt;=</span> <span class="m">0</span> <span class="o">?</span> <span class="nx">d</span> <span class="o">:</span> <span class="m">1</span><span class="o">/</span><span class="m">0</span><span class="o">;</span>

            <span class="c">// process the input</span>
<span class="c"></span>            <span class="k">return</span> <span class="nx">_stringify</span><span class="o">({</span><span class="s1">&#39;&#39;</span><span class="o">:</span><span class="nx">o</span><span class="o">},</span><span class="s1">&#39;&#39;</span><span class="o">,</span><span class="nx">d</span><span class="o">,</span><span class="nx">w</span><span class="o">,[]);</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="kc">undefined</span><span class="o">;</span>
    <span class="o">},</span>

    <span class="c">/**</span>
<span class="c">     * Serializes a Date instance as a UTC date string.  Used internally by</span>
<span class="c">     * stringify.  Override this method if you need Dates serialized in a</span>
<span class="c">     * different format.</span>
<span class="c">     * @method dateToString</span>
<span class="c">     * @param d {Date} The Date to serialize</span>
<span class="c">     * @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ</span>
<span class="c">     * @static</span>
<span class="c">     */</span>
    <span class="nx">dateToString</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">d</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">function</span> <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="nx">v</span> <span class="o">&lt;</span> <span class="m">10</span> <span class="o">?</span> <span class="s1">&#39;0&#39;</span> <span class="o">+</span> <span class="nx">v</span> <span class="o">:</span> <span class="nx">v</span><span class="o">;</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="nx">d</span><span class="o">.</span><span class="nx">getUTCFullYear</span><span class="o">()</span>         <span class="o">+</span> <span class="s1">&#39;-&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">d</span><span class="o">.</span><span class="nx">getUTCMonth</span><span class="o">()</span> <span class="o">+</span> <span class="m">1</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;-&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">d</span><span class="o">.</span><span class="nx">getUTCDate</span><span class="o">())</span>      <span class="o">+</span> <span class="s1">&#39;T&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">d</span><span class="o">.</span><span class="nx">getUTCHours</span><span class="o">())</span>     <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">d</span><span class="o">.</span><span class="nx">getUTCMinutes</span><span class="o">())</span>   <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">d</span><span class="o">.</span><span class="nx">getUTCSeconds</span><span class="o">())</span>   <span class="o">+</span> <span class="s1">&#39;Z&#39;</span><span class="o">;</span>
    <span class="o">},</span>

    <span class="c">/**</span>
<span class="c">     * Reconstitute Date instances from the default JSON UTC serialization.</span>
<span class="c">     * Reference this from a reviver function to rebuild Dates during the</span>
<span class="c">     * parse operation.</span>
<span class="c">     * @method stringToDate</span>
<span class="c">     * @param str {String} String serialization of a Date</span>
<span class="c">     * @return {Date}</span>
<span class="c">     */</span>
    <span class="nx">stringToDate</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">str</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="sr">/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">str</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">var</span> <span class="nx">d</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="o">();</span>
            <span class="nx">d</span><span class="o">.</span><span class="nx">setUTCFullYear</span><span class="o">(</span><span class="nb">RegExp</span><span class="o">.</span><span class="nx">$1</span><span class="o">,</span> <span class="o">(</span><span class="nb">RegExp</span><span class="o">.</span><span class="nx">$2</span><span class="o">|</span><span class="m">0</span><span class="o">)-</span><span class="m">1</span><span class="o">,</span> <span class="nb">RegExp</span><span class="o">.</span><span class="nx">$3</span><span class="o">);</span>
            <span class="nx">d</span><span class="o">.</span><span class="nx">setUTCHours</span><span class="o">(</span><span class="nb">RegExp</span><span class="o">.</span><span class="nx">$4</span><span class="o">,</span> <span class="nb">RegExp</span><span class="o">.</span><span class="nx">$5</span><span class="o">,</span> <span class="nb">RegExp</span><span class="o">.</span><span class="nx">$6</span><span class="o">);</span>
            <span class="k">return</span> <span class="nx">d</span><span class="o">;</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nx">str</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">};</span>

<span class="o">})();</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div id="moduleList" class="module">
                        <h4>Modules</h4>
                        <ul class="content">
                                <li class=""><a href="module_animation.html" title="animation">animation</a></li>
                                <li class=""><a href="module_autocomplete.html" title="autocomplete">autocomplete</a></li>
                                <li class=""><a href="module_button.html" title="button">button</a></li>
                                <li class=""><a href="module_calendar.html" title="calendar">calendar</a></li>
                                <li class=""><a href="module_carousel.html" title="carousel">carousel</a></li>
                                <li class=""><a href="module_charts.html" title="charts">charts</a></li>
                                <li class=""><a href="module_colorpicker.html" title="colorpicker">colorpicker</a></li>
                                <li class=""><a href="module_connection.html" title="connection">connection</a></li>
                                <li class=""><a href="module_container.html" title="container">container</a></li>
                                <li class=""><a href="module_cookie.html" title="cookie">cookie</a></li>
                                <li class=""><a href="module_datasource.html" title="datasource">datasource</a></li>
                                <li class=""><a href="module_datatable.html" title="datatable">datatable</a></li>
                                <li class=""><a href="module_dom.html" title="dom">dom</a></li>
                                <li class=""><a href="module_dragdrop.html" title="dragdrop">dragdrop</a></li>
                                <li class=""><a href="module_editor.html" title="editor">editor</a></li>
                                <li class=""><a href="module_element.html" title="element">element</a></li>
                                <li class=""><a href="module_event.html" title="event">event</a></li>
                                <li class=""><a href="module_get.html" title="get">get</a></li>
                                <li class=""><a href="module_history.html" title="history">history</a></li>
                                <li class=""><a href="module_imagecropper.html" title="imagecropper">imagecropper</a></li>
                                <li class=""><a href="module_imageloader.html" title="imageloader">imageloader</a></li>
                                <li class="selected"><a href="module_json.html" title="json">json</a></li>
                                <li class=""><a href="module_layout.html" title="layout">layout</a></li>
                                <li class=""><a href="module_logger.html" title="logger">logger</a></li>
                                <li class=""><a href="module_menu.html" title="menu">menu</a></li>
                                <li class=""><a href="module_paginator.html" title="paginator">paginator</a></li>
                                <li class=""><a href="module_profiler.html" title="profiler">profiler</a></li>
                                <li class=""><a href="module_profilerviewer.html" title="profilerviewer">profilerviewer</a></li>
                                <li class=""><a href="module_resize.html" title="resize">resize</a></li>
                                <li class=""><a href="module_selector.html" title="selector">selector</a></li>
                                <li class=""><a href="module_slider.html" title="slider">slider</a></li>
                                <li class=""><a href="module_stylesheet.html" title="stylesheet">stylesheet</a></li>
                                <li class=""><a href="module_tabview.html" title="tabview">tabview</a></li>
                                <li class=""><a href="module_treeview.html" title="treeview">treeview</a></li>
                                <li class=""><a href="module_uploader.html" title="uploader">uploader</a></li>
                                <li class=""><a href="module_yahoo.html" title="yahoo">yahoo</a></li>
                                <li class=""><a href="module_yuiloader.html" title="yuiloader">yuiloader</a></li>
                                <li class=""><a href="module_yuitest.html" title="yuitest">yuitest</a></li>
                        </ul>
                    </div>

                    <div id="classList" class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="JSON.html" title="JSON">JSON</a></li>
                        </ul>
                    </div>

                    <div id="fileList" class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class="selected"><a href="JSON.js.html" title="JSON.js">JSON.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2009 Yahoo! Inc. All rights reserved.
	</div>
</div>
<script type="text/javascript">
    ALL_YUI_PROPS = [{"access": "private", "host": "JSON", "name": "_BRACKETS", "url": "JSON.html#property__BRACKETS", "type": "property"}, {"access": "", "host": "JSON", "name": "_char", "url": "JSON.html#method__char", "type": "method"}, {"access": "private", "host": "JSON", "name": "_CHARS", "url": "JSON.html#property__CHARS", "type": "property"}, {"access": "", "host": "JSON", "name": "dateToString", "url": "JSON.html#method_dateToString", "type": "method"}, {"access": "private", "host": "JSON", "name": "_ESCAPES", "url": "JSON.html#property__ESCAPES", "type": "property"}, {"access": "private", "host": "JSON", "name": "_INVALID", "url": "JSON.html#property__INVALID", "type": "property"}, {"access": "", "host": "JSON", "name": "_isValid", "url": "JSON.html#method__isValid", "type": "method"}, {"access": "", "host": "JSON", "name": "isValid", "url": "JSON.html#method_isValid", "type": "method"}, {"access": "", "host": "JSON", "name": "parse", "url": "JSON.html#method_parse", "type": "method"}, {"access": "private", "host": "JSON", "name": "_prepare", "url": "JSON.html#method__prepare", "type": "method"}, {"access": "private", "host": "JSON", "name": "_revive", "url": "JSON.html#method__revive", "type": "method"}, {"access": "private", "host": "JSON", "name": "_SPECIAL_CHARS", "url": "JSON.html#property__SPECIAL_CHARS", "type": "property"}, {"access": "private", "host": "JSON", "name": "_string", "url": "JSON.html#method__string", "type": "method"}, {"access": "", "host": "JSON", "name": "_stringify", "url": "JSON.html#method__stringify", "type": "method"}, {"access": "", "host": "JSON", "name": "stringify", "url": "JSON.html#method_stringify", "type": "method"}, {"access": "", "host": "JSON", "name": "stringToDate", "url": "JSON.html#method_stringToDate", "type": "method"}, {"access": "private", "host": "JSON", "name": "_UNICODE_EXCEPTIONS", "url": "JSON.html#property__UNICODE_EXCEPTIONS", "type": "property"}, {"access": "private", "host": "JSON", "name": "_VALUES", "url": "JSON.html#property__VALUES", "type": "property"}];
</script>
</body>
</html>
